﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using JustMVCTest.Models;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace JustMVCTest.Controllers
{
    public class POsEditController : Controller
    {
        private PODBContext db2 = new PODBContext();
        //
        // GET: /POsList/

        public ActionResult Edit(int Item, int PONumber)
        {
            PO po = db2.POs.Find(PONumber, Item);
            if (po == null)
            {
                return HttpNotFound();
            }
            return View(po);
        }

        [HttpPost]
        public ActionResult Edit(PO po)
        {
            if (ModelState.IsValid)
            {
                db2.Entry(po).State = EntityState.Modified;
                db2.SaveChanges();

                var client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential("papiproj@gmail.com", "0545357710"),
                    EnableSsl = true
                };
               
                client.Send("papiproj@gmail.com", "flame2262@gmail.com", "test", "Dear User, \n The PO: "+po.PONumber.ToString()+" has recieved a postponment for delivery date to date: "+po.DeliveryDate.ToString()+"\n Thank you and have a pleasant week, \n PAPI SYSTEM ");
                Console.WriteLine("Sent");
                Console.ReadLine();



                return RedirectToAction("Index","POsList");
            }
            return View(po);
        }
    }
}
